//
//  JJSearchProvinceTableController.m
//  Group4KY
//
//  Created by 一介布衣 on 16/4/6.
//  Copyright © 2016年 HUAMANLOU. All rights reserved.
//

#import "JJSearchProvinceTableController.h"
#import "JJCitiesTableViewController.h"
#import "JJSearchResultTableController.h"
#import "CityModel.h"
#import <AFNetworking.h>
static NSString *searchProvinceCellId = @"searchProvinceCellId";

@interface JJSearchProvinceTableController ()<UISearchResultsUpdating,UISearchControllerDelegate,UISearchBarDelegate>

/// 省份数据
@property (nonatomic,strong) NSMutableArray *provincesArray;
/// 搜索条
@property (nonatomic,strong) UISearchController *searchController;
/// 搜索省份的结果
@property (nonatomic,strong) JJSearchResultTableController *searchResultTableVC;
/// 选中的省份模型
@property (nonatomic,strong) NSMutableArray *resultProvinces;
@property(nonatomic,copy)NSString *provinceCode;
@end

@implementation JJSearchProvinceTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"省份";
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:searchProvinceCellId];
  
    self.searchResultTableVC = [[JJSearchResultTableController alloc] initWithStyle:UITableViewStyleGrouped];
    self.searchResultTableVC.searchProvinceNav = self.navigationController;
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:self.searchResultTableVC];
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nav];
    
    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.searchController.searchResultsUpdater = self;
    
    self.searchController.delegate = self;
    self.searchController.searchBar.delegate = self;
    self.searchController.searchBar.placeholder = @"请输入您要搜索的省份";
    self.definesPresentationContext = YES;  // 解决headView的高度无法自定义的问题
    [self getProvinceData];
}
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}



- (NSMutableArray *)resultProvinces {
    if (_resultProvinces == nil) {
        _resultProvinces = [NSMutableArray array];
    }
    return _resultProvinces;
}



-(void)getProvinceData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //        http://120.77.213.62:9080
    NSString *url =ServiceUrl@"/areaProvinceCityApp/searchProvince";
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dict in array) {
            CityModel *model = [[CityModel alloc] initWithDictionry:dict];
            NSLog(@"%@",model.province);
            [self.provincesArray addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.provincesArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:searchProvinceCellId forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    CityModel *model = self.provincesArray[indexPath.row];
    NSLog(@"%@",model.province);
    cell.textLabel.text = model.province;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CityModel *model = self.provincesArray[indexPath.row];
    JJCitiesTableViewController *vc = [[JJCitiesTableViewController alloc] init];
    vc.provinceCode = model.provinceCode;
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark - 更新搜索结果
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",self.searchController.searchBar.text];
    
    NSMutableArray *proviceNameList = [NSMutableArray array];
    for (CityModel *model in self.provincesArray) {
        [proviceNameList addObject:model.province];
    }
    
    
    // arr中存的只有省份的name
    NSArray *arr = [NSArray arrayWithArray:[proviceNameList filteredArrayUsingPredicate:predicate]];
    
    
    for (CityModel *model in self.provincesArray) {
        for (NSString *provinceName in arr) {
            if ([model.province isEqualToString:provinceName]) {
                [self.resultProvinces addObject:model];
                self.provinceCode = model.provinceCode;
            }
        }
    }
    self.searchResultTableVC.provinceNamesList = arr;
    self.searchResultTableVC.resultProvinces = self.resultProvinces.copy;
    self.searchResultTableVC.provinceCode = self.provinceCode;
    [self.searchResultTableVC.tableView reloadData];
}

-(NSMutableArray *)provincesArray{
    if (_provincesArray == nil) {
        _provincesArray = [[NSMutableArray alloc] init];
    }
    return _provincesArray;
}

#pragma mark - UISearchControllerDelegate代理方法
- (void)didDismissSearchController:(UISearchController *)searchController {
    if (searchController.searchBar.text.length == 0) {
        return;
    }
    [self.resultProvinces removeAllObjects];
}



#pragma mark - UISearchBarDelegate代理方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (searchText.length == 0) {
        return;
    }

    [self.resultProvinces removeAllObjects];
}

@end








